<?php
/*
 * File: Controller.php
 * Created Time: 2024-05-29 11:27:52 am
 * Author: WangYuQiang && WangpeiGang (719404171@qq.com)
 * -----
 * Last Modified: 2024-07-18 1:58:54 pm
 * Modified By: WangYuQiang (719404171@qq.com>)
 * -----
 * Copyright (c) 2024 成都睿創科技有限公司
 */


namespace App\Http\Controllers;

use OSS\OssClient;
use OSS\Core\OssException;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Common\MonologsController as Logs;//日志类
use App\Http\Controllers\Common\GoogleTranslateController as GoogleTranslate;//谷歌翻译

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;



    

/*********************************************************return common**********************************************************************/

    /**
    * 成功返回数据
    * @param $message 信息
    */
    protected function success($msg = "Success" , $results=''){

        // $results=GoogleTranslate::translate($results, 'en');

        return ["status" => 200, "msg" => $msg, "results" => $results];
    }


    /**
    * 失败返回数据
    * @param $message 信息
    */
    protected function error($msg = "Network Error" , $results=''){

        if(empty($results)){
            return ["status" => 500, "msg" => $msg];
        }else{
            return ["status" => 500, "msg" => $msg ,"results" => $results];
        }


    }


    /**
    * 参数错误返回数据
    * @param $message 信息
    */
    protected function parametersError($msg = "No Parameters" , $results=''){

        if(empty($results)){
            return ["status" => 501, "msg" => $msg];
        }else{
            return ["status" => 501, "msg" => $msg ,"results" => $results];
        }

    }



    /**
    * 输出日志信息
    * @param $message 信息
    * @param array $data 数据
    * @param string $filename 文件名称  默认：info
    * @param string $isDate 是否按月份分文件夹 默认：否
    */
    protected function LogInfo($message, $data, $filename, $isDate){

        Logs::info($message, json_encode($data), $filename, $isDate);
    }


/*********************************************************return web**********************************************************************/

    protected static function getLanguageWeb(){
        $header = getallheaders();
        if($header['Language'] == 'en'){
            $language = 'en_';
        }else{
            $language = '';
        }
        return $language;
    }



/*********************************************************return api**********************************************************************/


 
    /**
     * @param $value
     * @return array
     * 獲取二維碼
     */
    public function getQRCode($value){
        $img = new \QRcode();
        $values = $value; //二维码内容
        $errorCorrectionLevel = 'L';//容错级别
        $matrixPointSize = 6; // 生成图片大小
        $filename = rand(10000, 99999) . time() . '.jpg';
        $img->png($values, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
        $uploadQRcode = $this->uploadQRcode($filename);
        if (!empty($uploadQRcode)) {
            @unlink($filename);//刪除生產二維碼
            return $uploadQRcode;
        } else {
            $file_path='';
            return $file_path;
        }

    }


    /**
     * @param $filename
     * @return array
     * 上傳二維碼
     */
    public function uploadQRcode($filename){
        $accessKeyId = "LTAI4G2Np2fQbQ4SqfZbqXk7";
        $accessKeySecret = "eqtOSuDazSrLSPKTc0IZjMcrJfFjmE";
        $endpoint = "oss-cn-hongkong.aliyuncs.com";
        // 存储空间名称
        $bucket = "brain-storm";
        $object = 'ewm/' . $filename;
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ossClient->uploadFile($bucket, $object, $filename);
            $file_path='https://brain-storm.oss-cn-hongkong.aliyuncs.com/' . $object;
            return $file_path;
        } catch (OssException $e) {
            return '';
        }
    }






/*********************************************************END************************************************************************/


}
